import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {List, Detail} from './DTO';
const baseUrl = '/afterSales/api/v1';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: 'SOC'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const updateInfoDesc = formatMessage({
    id: 'api.message.updateInfoDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const submitInfoDesc = formatMessage({
    id: 'api.message.submitInfoDesc',
    defaultMessage: '提交{resourceName}'
}, {resourceName});

const releaseInfoDesc = formatMessage({
    id: 'api.message.releaseInfoDesc',
    defaultMessage: '发布{resourceName}'
}, {resourceName});

const closeDesc = formatMessage({
    id: 'api.message.closeDesc',
    defaultMessage: '关闭{resourceName}'
}, {resourceName});

const initQueryDesc = formatMessage({
    id: 'api.message.initQueryDesc',
    defaultMessage: '初始化'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/brands`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, initQueryDesc));

// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/socs?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id => request(`${baseUrl}/socs/${id}`, null, Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));

// 编辑
export const updateInfo = (id, data) => request(`${baseUrl}/socs/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateInfoDesc));

// 提交
export const submitInfo = (id, data) => request(`${baseUrl}/socs/${id}/submit`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitInfoDesc));

// 发布
export const releaseInfo = (id, data) => request(`${baseUrl}/socs/${id}/release`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, releaseInfoDesc));

// 关闭
export const closeInfo = (id, data) => request(`${baseUrl}/socs/${id}/close`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, closeDesc));
